/*******************************************************************************
 * Package: com.hngtrust.iir.user.service.impl
 * Type:    SearchHistoryServiceImpl
 * Date:    2021-11-5 15:48
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.service.impl;

import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.user.controller.dto.AddHistoryReqVO;
import com.hngtrust.iir.user.entity.SearchHistory;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.SearchHisService;
import com.hngtrust.iir.user.service.SearchHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 搜索历史
 * @author Chentianyong
 * @date 2021-11-5 15:48
 */
@Service
@Slf4j
public class SearchHisServiceImpl implements SearchHisService {
    @Autowired
    private SearchHistoryService searchHistoryService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @Override
    public void addHistory(AddHistoryReqVO vo) {
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        SearchHistory req = new SearchHistory();
        req.setDataType(vo.getDataType());
        req.setSearchKey(vo.getSearchKey());
        req.setUserId(loginUser.getId());
        List<SearchHistory> list = searchHistoryService.queryList(req);
        if(list.isEmpty()){
            req.setId(myIdGenerator.nextId());
            req.setCreateTime(new Date());
            req.setCreateUserId(loginUser.getId());
        
            searchHistoryService.add(req);
        }
        else{
            SearchHistory sh = list.get(0);
            req.setId(sh.getId());
            req.setCreateTime(new Date());
            searchHistoryService.modifyById(req);
        }
    }
}
